﻿using CaptchaGen;
using HPIT.RentHouse.Common;
using RentHouse.DTO;
using RentHouse.IService;
using RentHouse.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace RentHouse.Admin.Controllers
{
    public class HomeController : Controller
    {
        private IAdminUsersService _adminUsersService;
        public HomeController(IAdminUsersService adminUsersService)
        {
            _adminUsersService = adminUsersService;
        }
        
        // GET: Home
        [Authorize]
        public ActionResult Index()
        {
            //IPermissionService permissionService = new PermissionService();
            //var list = permissionService.GetList();
            return View();
        }

        public ActionResult Login()
        {
            return View();
        }

        public ActionResult GetVerCode()
        {
            string code = CommonHelper.CreateVerifyCode(4);
            TempData["code"] = code;
            var src = ImageFactory.GenerateImage(code, 60, 100, 15, 6);
            return File(src, "images/jpeg");
        }

        [HttpPost]
        public ActionResult SubmitLogin(LoginDTO loginDTO)
        {
            if (TempData["code"] == null)
            {
                return Json(new AjaxResult(ResultState.Error, "验证码已失效"));
            }
            else
            {
                string code = TempData["code"].ToString();
                if (code != loginDTO.VerCode)
                {
                    return Json(new AjaxResult(ResultState.Error, "验证码有误"));
                }
                else
                {
                    AjaxResult result = _adminUsersService.Login(loginDTO);
                    return Json(result);
                }
            }
        }

        //退出登录
        public ActionResult Logout()
        {
            FormsAuthentication.SignOut();
            return RedirectToAction("Login");
        }

        public ActionResult NoPermission()
        {
            return View();
        }
    }
}